package com.template.mongo.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;

import com.template.mongo.persistence.entity.Account;

@SuppressWarnings("deprecation")
public final class UserUtils {
	public static String ROLE_ANNONYMOUS = "ROLE_ANNONYMOUS";
    public static String ROLE_USER_LOCKED = "ROLE_USER_LOCKED";
    public static String ROLE_LEADER = "ROLE_LEADER";
    public static String ROLE_USER = "ROLE_USER";
    public static String ROLE_ADVANCEUSER = "ROLE_ADVANCEUSER";
    public static String ROLE_SUPERADMIN = "ROLE_SUPERADMIN";
    public static String ROLE_MANAGER = "ROLE_MANAGER";
    public static String ROLE_ADMIN = "ROLE_ADMIN";
    public static String ROLE_OPENUSER = "ROLE_OPENUSER";
    public static String ROLE_CONTENT = "ROLE_CONTENT";
    
    public static String KEY_ROOTNAME = "root.username";
    public static String KEY_ROOTPWD = "root.password";

    /*
     * Get role name depending on level of user
     */
    public static List<GrantedAuthority> getRoleName(int level) {
        List<GrantedAuthority> lstGranted = new ArrayList<GrantedAuthority>();
        
        switch (level) {
        	case 0:
        		lstGranted.add(new GrantedAuthorityImpl(ROLE_OPENUSER));
    			break;
            case 1:
            	lstGranted.add(new GrantedAuthorityImpl(ROLE_USER));
            	break;
            case 2:
                lstGranted.add(new GrantedAuthorityImpl(ROLE_USER));
                lstGranted.add(new GrantedAuthorityImpl(ROLE_ADVANCEUSER));
                break;
            case 3:
                lstGranted.add(new GrantedAuthorityImpl(ROLE_USER));
                lstGranted.add(new GrantedAuthorityImpl(ROLE_ADMIN));
                break;
            case 4:
                lstGranted.add(new GrantedAuthorityImpl(ROLE_USER));
                lstGranted.add(new GrantedAuthorityImpl(ROLE_ADVANCEUSER));
                lstGranted.add(new GrantedAuthorityImpl(ROLE_MANAGER));
                break;
            case 5:
                lstGranted.add(new GrantedAuthorityImpl(ROLE_USER));
                lstGranted.add(new GrantedAuthorityImpl(ROLE_ADVANCEUSER));
                lstGranted.add(new GrantedAuthorityImpl(ROLE_SUPERADMIN));
                break;
            case 6:
            	lstGranted.add(new GrantedAuthorityImpl(ROLE_USER));
            	lstGranted.add(new GrantedAuthorityImpl(ROLE_CONTENT));
            	break;
            default:
                lstGranted.add(new GrantedAuthorityImpl(ROLE_ANNONYMOUS));
                break;
        }
        return lstGranted;
    }

    /*
     * Create annonymous user
     */
    public static User createAnnonymous() {
        List<GrantedAuthority> lstGranted = new ArrayList<GrantedAuthority>();
        lstGranted.add(new GrantedAuthorityImpl(ROLE_ANNONYMOUS));
        return new User("annonymous", "", false, true, true, true, lstGranted);
    }
    
    /**
     * @param usrName
     * @return User for open id
     */
    public static User createOpenUser(String usrName, String pwd) {
    	return new User(usrName, pwd, true, true, true, true, getRoleName(0));
    }

    /*
     * Create a user who is inactive
     */
    public static User createLockUser(Account user) {
        List<GrantedAuthority> lstGranted = new ArrayList<GrantedAuthority>();
        lstGranted.add(new GrantedAuthorityImpl(ROLE_USER_LOCKED));
        return new User(user.getUserName(), user.getPassword(),
                false, false, false, false, lstGranted);
    }
    
    /*
     * Create an active user
     */
    public static User createActiveUser(Account user) {
        return new User(user.getUserName(), user.getPassword(),
                true, true, true, true, getRoleName(user.getLevel()));
    }
    
    public static User createAdvanceAdmin(String sa, String saPwd) {
        return new User(sa, saPwd, true, true, true, true, getRoleName(5));
    }
}
